﻿using HomagChina.Basic.CCT.Tools;
using HomagChina.CCHCS.Views;
using HomagGroup.Base.UI;
using Proxy;
using System.Collections.Generic;
using System.Linq;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class BoardPreviewViewModel : NotifyPropertyChangedBase
    {
        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private CommonHandle Handle_Common;
        public RelayCommand NextCommand { get; set; }

        private string _Title;

        public string Title
        {
            get => _Title;
            set { _Title = value; OnPropertyChanged(); }
        }

        private List<BoardInfoConstruct> _List_BoardInfoModel;

        public List<BoardInfoConstruct> List_BoardInfoModel
        {
            get => _List_BoardInfoModel;
            set { _List_BoardInfoModel = value; OnPropertyChanged(); }
        }

        public BoardPreviewViewModel(string fileName, string format)
        {
            Title = GetLocalText("Preview");
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Common = new CommonHandle();
            NextCommand = new RelayCommand(Next);
            Init(fileName, format);
        }

        public void Init(string fileName, string format)
        {
            List_BoardInfoModel = new DealWithFiles().DealWithBoardInfoFiles(fileName, ',', format);
        }

        public void Next()
        {
            if (List_BoardInfoModel != null)
            {
                Returnstructure4Boardlibrary re = new Returnstructure4Boardlibrary();
                re = Handle_Search.ChckeBoardsInfo(List_BoardInfoModel);
                if (re.repetitious.Count() > 0)
                {
                    BoardRepetitiousViewModel boardRepetitiousViewModel = new BoardRepetitiousViewModel(re);
                    UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardRepetitious>(boardRepetitiousViewModel, null);
                }
                else
                {
                    List<BoardInfoConstruct> list = new List<BoardInfoConstruct>();
                    foreach (var item in re.boardLibraries)
                    {
                        BoardInfoConstruct t = Handle_Common.Mapper(new BoardInfoConstruct(), item);
                        list.Add(t);
                    }
                    Handle_Insert.InsertBoardsInfo(list, null);
                    HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Congratulations"));
                }
            }
            else
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show("当前文件内无内容！");
            }
            Handle_Common.CloseDiaLog(Title);
        }
    }
}